package com.xwtec.crawler.temp;

import gui.ava.html.image.generator.HtmlImageGenerator;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.UUID;

public class Temp {
    public static void main(String[] args) throws Exception {
//        List<String> urls = Arrays.asList("http://www.baidu.com", "http://www.sohu.com", "http://www.sina.com");
//        for (int i=0; i<urls.size(); i++){
//            image1(urls.get(i));
//        }

        //image2("http://www.163.com");

        image3("http://www.baidu.com");
    }

    public static void image1(String url) throws UnknownHostException, IOException, AWTException, URISyntaxException{
        long start = System.currentTimeMillis();
        //此方法仅适用于JdK1.6及以上版本
        Desktop.getDesktop().browse(
                new URL(url).toURI());
        Robot robot = new Robot();
        robot.delay(5000);
        Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
        int width = (int) d.getWidth();
        int height = (int) d.getHeight();
        //最大化浏览器
        robot.keyRelease(KeyEvent.VK_F11);
        robot.delay(2000);
        Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
                height));
        BufferedImage bi = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        //保存图片
        ImageIO.write(bi, "jpg", new File(UUID.randomUUID()+".jpg"));
        long end = System.currentTimeMillis();
        System.out.println("timg===>"+(end-start));
    }

    public static void image2(String url){
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        imageGenerator.loadUrl(url);
        imageGenerator.saveAsImage(System.currentTimeMillis()+".png");
        imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
    }

    public static void image3(String url) throws Exception {
        JEditorPane ed = new JEditorPane(new URL(url));
        ed.setSize(2000,2000);

        //create a new image
        BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        //paint the editor onto the image
        SwingUtilities.paintComponent(image.createGraphics(),
                ed,
                new JPanel(),
                0, 0, image.getWidth(), image.getHeight());
        //save the image to file
        ImageIO.write((RenderedImage)image, "png", new File("html.png"));
    }
}
